<template>
	<view class="addGift">
		<u--form :model="formData" :rules="rules" ref="uForm" labelWidth="200rpx">
			<u-form-item label="姓名:" prop="name" borderBottom required>
				<u--input v-model="formData.name" placeholder="请填写名字" clearable
					:customStyle="{border:'2rpx solid #ddd'}"></u--input>
			</u-form-item>
			<u-form-item label="金额:" prop="money" borderBottom required>
				<u--input v-model="formData.money" type="digit" placeholder="请填写金额" clearable
					:customStyle="{border:'2rpx solid #ddd'}"></u--input>
			</u-form-item>
			<u-form-item label="随礼人地址:" prop="address" borderBottom required>
				<u--input v-model="formData.address" placeholder="请填写地址" clearable
					:customStyle="{border:'2rpx solid #ddd'}"></u--input>
			</u-form-item>
			<u-form-item label="备注:" prop="remark" borderBottom>
				<u--textarea v-model="formData.remark" placeholder="请输入备注" autoHeight clearable
					:customStyle="{border:'2rpx solid #ddd'}"></u--textarea>
			</u-form-item>
		</u--form>
		<u-button type="primary" text="提交" :customStyle="{marginTop:'80rpx'}" @click="submit"></u-button>
	</view>
</template>

<script>
	import {
		Gift
	} from '@/static/utils/service.js'
	import {
		post
	} from '@/static/utils/request.js'
	import {
		Routes,
		Toast
	} from '../../static/utils/utils'
	export default {
		data() {
			return {
				gift_book_id: '',
				formData: {
					name: '',
					money: '',
					address: '',
					remark: ''
				},
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请填写名字',
						trigger: ['blur', 'change']
					},
					'money': [{
						required: true,
						message: '请填写金额',
						trigger: ['blur', 'change']
					}, {
						type: 'number',
						message: '金额必须为数字',
						trigger: ['blur', 'change']
					}],
					'address': {
						type: 'string',
						required: true,
						message: '请填写地址',
						trigger: ['blur', 'change']
					}
				},
			}
		},
		onLoad(e) {
			this.gift_book_id = e.gift_book_id
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			submit() {
				this.$refs.uForm.validate().then(async () => {
					const {
						code,
						msg
					} = await post(Gift.add, {
						...this.formData,
						gift_book_id: this.gift_book_id
					}, '提交中')
					if (code === 1) {
						Toast.success('提交成功')
						setTimeout(() => {
							Routes.back()
						}, 1500)
					} else Toast.error(msg)
				}).catch(errors => {
					Toast.error('请完善信息')
				})
			}
		}
	}
</script>

<style lang="scss">
	.addGift {
		padding: 0 25rpx;
	}
</style>